﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TSV.Models;
using TSV.Library.Common;
using TSV.Areas.Administration.Models;

namespace TSV.Library.GenericHandle
{
    /// <summary>
    /// Summary description for AutoCompleteHandler
    /// </summary>
    public class AutoCompleteHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string name = context.Request.QueryString["q"];
            string pageName = context.Request.QueryString["Page"];
            switch (pageName)
            {
                case "UserAdmin":
                        UserAdminDao userAdminDao = new UserAdminDao();
                        List<UserAdmin> listUserAdmin = userAdminDao.GetList().Where(p => p.UserName.ToLower().Contains(name)).ToList<UserAdmin>();
                        foreach (UserAdmin item in listUserAdmin)
                        {
                            context.Response.Write(item.UserName + Environment.NewLine);
                        }
                        break;
                case "Collaborator":
                        CollaboratorDao collaboratorDao = new CollaboratorDao();
                        List<Collaborator> listCollaborator = collaboratorDao.GetList().Where(p => p.FullName.ToLower().Contains(name)).ToList<Collaborator>();
                        foreach (Collaborator item in listCollaborator)
                        {
                            context.Response.Write(item.FullName + Environment.NewLine);
                        }
                        break;
                case "RegisterCard":
                        OwnerStudentCardDao OSCDao = new OwnerStudentCardDao();
                        List<sp_GetOwnerStudentCardResult> result = OSCDao.GetList(name);
                        List<String> lstDisplayName = result.Select(p => p.FullName).ToList();
                        foreach (string item in lstDisplayName)
                        {
                            context.Response.Write(item + Environment.NewLine);
                        }
                        break;
                case "News":
                        NewsDao newsDao = new NewsDao();
                        List<sp_GetNewsResult> listNews = newsDao.GetList(name, null, null, null).ToList();
                        foreach (sp_GetNewsResult item in listNews)
                        {
                            context.Response.Write(item.ID + Environment.NewLine);
                        }
                        break;
                case "Author":
                        List<sp_GetNewsResult> listAuthorNews = new NewsDao().GetList(null, name, null, null).ToList();
                        foreach (sp_GetNewsResult item in listAuthorNews)
                        {
                            context.Response.Write(item.Author + Environment.NewLine);
                        }
                        break;
                case "BusinessRegister":
                        BusinessRegisterDao BRDao = new BusinessRegisterDao();
                        List<sp_GetBusinessRegisterResult> BRList = BRDao.GetList(name);
                        List<String> lstBusinessName = BRList.Select(p => p.BusinessName).ToList();
                        foreach (string item in lstBusinessName)
                        {
                            context.Response.Write(item + Environment.NewLine);
                        }
                        break;
            }
           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}